<?php
namespace App\Server;

use EasySwoole\EasySwoole\ServerManager;

use App\Auth\Auth;
use App\Storage\OnlineUser;
use App\Storage\OnlineClient;

class WebServer extends Server
{
    /**
     * 解析Request, 分发方法
     *
     * @Author LYC 17750800820@163.com
     * @DateTime 2020-04-22 23:09:37
     * @return void
     */
    public function index()
    {
        $args = $this->caller()->getArgs();
        $client = $this->caller()->getClient();
        // 解析Request，分发方法
        $request = json_decode($args['request'], true);
        if (!empty($request['request'])) {
            switch ($request['request']) {
                case 'login':
                    $this->login($request, $client);
                break;
                // 添加一个人员数据
                case 'addUser':
                    $this->addUser($request, $client);
                break;
                // 更新一个人员数据
                case 'updateUser':
                    $this->updateUser($request, $client);
                break;
                // 更新一个人员数据
                case 'deleteUser':
                    $this->deleteUser($request, $client);
                break;
                // 推送指令包给设备，批量处理
                // case 'ApiRequestFileUrl':
                // break;
            }
        } elseif (!empty($request['live'])) {
            $this->heart($request, $client);
        }
    }

    /**
     * 登录,分配身份
     *
     * @Author LYC 17750800820@163.com
     * @DateTime 2020-04-27 12:20:31
     * @param array $request
     * @param  WebSocket    $client   WebSocket Client 对象
     * @return json
     */
    public function login($request, $client)
    {
        if (!empty($request['authorization']) && !empty($request['admin'])) {
            $auth = new Auth('Basic');
            $authorization = $auth->auth('webClient', 'webClient');
            if ($authorization == $request['authorization']) {
                // 为网页用户分配身份
                OnlineUser::getInstance()->set($fd, $request['name'], $authorization);
            }
            $response = [
                'response'=>'login',
                'result'=>'ok',
                'device'=>OnlineClient::getInstance()->getClients()
            ];
        } else {
            // 登录失败, 关闭该连接
            $response = [
                'response'=>'login',
                'result'=>'fail'
            ];
        }
        $response = json_encode($response);
        $this->response()->setMessage($response);
    }
    /**
     * 添加一个人员数据
     *
     * @Author LYC 17750800820@163.com
     * @DateTime 2020-04-22 23:53:10
     * @param array $request
     * @param  WebSocket    $client   WebSocket Client 对象
     * @return void
     */
    protected function addUser($request, $client)
    {
        $clientFd = OnlineClient::getInstance()->getClient($request['device_serial_no']);
        if ($clientFd != null) {
            $terminalServer = new \App\Server\TerminalServer;
            $terminalServer->addUser($request, $client, $clientFd);
            $response = [
                'response'=>'addUser',
                'message'=>'已向终端发送更新请求.'
            ];
        } else {
            $response = [
                'response'=>'addUser',
                'message'=>'已掉线, 请等待终端重连.'
            ];
        }
        $response = json_encode($response);
        $this->response()->setMessage($response);
    }

    /**
     * 更新一个人员数据
     *
     * @Author LYC 17750800820@163.com
     * @DateTime 2020-04-22 23:53:10
     * @param array $request
     * @param  WebSocket    $client   WebSocket Client 对象
     * @return void
     */
    protected function updateUser($request, $client)
    {
        $clientFd = OnlineUser::getInstance()->getClient($request['device_serial_no']);
        if ($clientFd != null) {
            $terminalServer = new \App\Server\TerminalServer;
            $terminalServer->addUser($request, $client);
            $response = [
                'status'=>'success',
                'message'=>'已向终端发送更新请求.'
            ];
        } else {
            $response = [
                'status'=>'failed',
                'message'=>'已掉线, 请等待终端重连.'
            ];
        }
        $response = json_encode($response);
        $this->response()->setMessage($response);
    }

    /**
     * 添加/更新一个人员数据
     *
     * @Author LYC 17750800820@163.com
     * @DateTime 2020-04-22 23:53:10
     * @param array $request
     * @param  WebSocket    $client   WebSocket Client 对象
     * @return void
     */
    protected function deleteUser($request, $client)
    {
        $clientFd = OnlineUser::getInstance()->getClient($request['device_serial_no']);
        if ($clientFd != null) {
            $terminalServer = new \App\Server\TerminalServer;
            $terminalServer->deleteUser($request, $client);
            $response = [
                'status'=>'success',
                'message'=>'已向终端发送删除请求.'
            ];
        } else {
            $response = [
                'status'=>'failed',
                'message'=>'已掉线, 请等待终端重连.'
            ];
        }
        $response = json_encode($response);
        $this->response()->setMessage($response);
    }
   
    /**
     * 心跳处理
     *
     * @Author LYC 17750800820@163.com
     * @DateTime 2020-04-24 11:24:24
     * @param array $request
     * @param  WebSocket    $client   WebSocket Client 对象
     * @return void
     */
    protected function heart($request, $client)
    {
        // 更新上次心跳
        OnlineUser::getInstance()->updateHeartbeat($client->getFd());
        $response = [
            'response'=>'live',
            'live' => 'pong'
        ];
        $response = json_encode($response);
        $this->response()->setMessage($response);
    }
}
